package com.zzyang.mybatis.jdbc.loadbalance;

import com.zzyang.mybatis.jdbc.config.IConstants;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 抽象层
 *
 * @author zhuoziyang
 * @date 2021/4/8 23:00
 */
public abstract class AbstractLoadBalance implements LoadBalance {

    @Override
    public String select(List<String> dataSourceIds) {
        if (CollectionUtils.isEmpty(dataSourceIds)) {
            return IConstants.NO_AVAILABLE;
        }
        if (dataSourceIds.size() == 1) {
            return dataSourceIds.get(0);
        }
        return doSelect(dataSourceIds);
    }

    /**
     * 长度大于 1 的 id
     *
     * @param dataSourceIds
     * @return
     */
    protected abstract String doSelect(List<String> dataSourceIds);

}
